<?php
/**
 * Created by PhpStorm.
 * User: 马超
 * Date: 2017/12/11
 * Time: 16:11
 */

namespace console\controllers;

use yii\console\Controller;
use yii\console\Exception;
use Beanstalk\Client;

class TestController extends Controller
{


    public function actionBeanstalkProducer(){
        $beanstalk = new Client(); // For connection options see the
        // class documentation.
        $beanstalk->connect();
        $beanstalk->useTube('flux'); // Begin to use tube `'flux'`.
        $beanstalk->put(
            1, // Give the job a priority of 23.
            30,  // Do not wait to put job into the ready queue.
            60, // Give the job 1 minute to run.
            '/path/to/cat-image.png' // The job's body.
        );
        $beanstalk->disconnect();
    }

    public function actionBeanstalkConsumer(){
        $beanstalk = new Client();
        $beanstalk->connect();
        $beanstalk->watch('flux');
        while (true) {
            $job = $beanstalk->reserve(); // Block until job is available.
            // Now $job is an array which contains its ID and body:
            // ['id' => 123, 'body' => '/path/to/cat-image.png']
            // Processing of the job...
            echo($job['body']);
            $beanstalk->delete($job['id']);
//            if ($result) {
//                $beanstalk->delete($job['id']);
//            } else {
//                $beanstalk->bury($job['id'],0);
//            }
        }
    }


}